// component/calendar/calendar.js
Component({
    properties: {
      // 是否显示日历
      visible: {
        type: Boolean,
        value: false
      },
      // 默认选中日期
      defaultDate: {
        type: String, // 格式: YYYY-MM-DD
        value: ''
      }
    },
    data: {
      currentYear: 0,
      currentMonth: 0,
      days: [], // 当月日期列表
      selectedDate: '' // 当前选中日期
    },
    lifetimes: {
      attached() {
        // 初始化日期
        const today = this.data.defaultDate ? new Date(this.data.defaultDate) : new Date();
        this.setData({
          currentYear: today.getFullYear(),
          currentMonth: today.getMonth() + 1,
          selectedDate: this.data.defaultDate || this.formatDate(today)
        });
        this.generateDays();
      }
    },
    methods: {
      // 生成当月日期列表
      generateDays() {
        const { currentYear, currentMonth } = this.data;
        const daysInMonth = new Date(currentYear, currentMonth, 0).getDate(); // 当月天数
        const firstDay = new Date(currentYear, currentMonth - 1, 1).getDay(); // 当月第一天是星期几
        
        const days = [];
        // 添加上月剩余天数（占位）
        for (let i = 0; i < firstDay; i++) {
          days.push({ day: '', isCurrentMonth: false });
        }
        // 添加当月天数
        for (let i = 1; i <= daysInMonth; i++) {
          const date = `${currentYear}-${this.padZero(currentMonth)}-${this.padZero(i)}`;
          days.push({
            day: i,
            isCurrentMonth: true,
            date,
            isSelected: date === this.data.selectedDate,
            isToday: date === this.formatDate(new Date())
          });
        }
        this.setData({ days });
      },
      
      // 格式化日期为 YYYY-MM-DD
      formatDate(date) {
        return `${date.getFullYear()}-${this.padZero(date.getMonth() + 1)}-${this.padZero(date.getDate())}`;
      },
      
      // 数字补零
      padZero(num) {
        return num < 10 ? `0${num}` : num;
      },
      
      // 切换到上月
      prevMonth() {
        let { currentYear, currentMonth } = this.data;
        if (currentMonth === 1) {
          currentMonth = 12;
          currentYear--;
        } else {
          currentMonth--;
        }
        this.setData({ currentYear, currentMonth });
        this.generateDays();
      },
      
      // 切换到下月
      nextMonth() {
        let { currentYear, currentMonth } = this.data;
        if (currentMonth === 12) {
          currentMonth = 1;
          currentYear++;
        } else {
          currentMonth++;
        }
        this.setData({ currentYear, currentMonth });
        this.generateDays();
      },
      
      // 选择日期
      selectDay(e) {
        const { date } = e.currentTarget.dataset;
        if (!date) return;
        
        this.setData({ selectedDate: date });
        // 更新日期选中状态
        this.data.days.forEach(day => {
          day.isSelected = day.date === date;
        });
        this.setData({ days: this.data.days });
      },
      
      // 确认选择
      confirm() {
        this.triggerEvent('confirm', { date: this.data.selectedDate });
        this.setData({ visible: false });
      },
      
      // 取消选择
      cancel() {
        this.setData({ visible: false });
      }
    }
  })
  